<?php
namespace app\livevideo\model;

use app\BaseModel;
use think\facade\Db;

class LiveVideo extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_livevideo_list';


    protected $append = [

        'time_length'

    ];

    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:57
     * @功能说明:时长
     */
    public function getTimeLengthAttr($value,$data){


        $date =  lbGetDatess($data['end_time']-$data['start_time']);

        return str_replace('还剩','',$date);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 18:38
     * @功能说明:开始时间
     */
    public function getStartTimeAttr($value,$data){

        return date('Y-m-d H:i:s',$value);
    }
    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:03
     * @功能说明:初始化数据
     */
    public function initData($data,$uniacid){

        $this->liveDel(['uniacid'=>$uniacid]);

        if(!empty($data)){

            $list = [];

            foreach ($data as $k=>$v){

                $list[$k]['uniacid']     = $uniacid;

                $list[$k]['roomid']      = $v['roomid'];

                $list[$k]['name']        = $v['name'];

                $list[$k]['share_img']   = $v['share_img'];

                $list[$k]['start_time']  = $v['start_time'];

                $list[$k]['live_status'] = $v['live_status'];

                $list[$k]['end_time']    = $v['end_time'];

                $list[$k]['cover_img']   = $v['cover_img'];

                $list[$k]['anchor_name'] = $v['anchor_name'];

            }
            $this->saveAll($list);
        }
        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:12
     * @功能说明:直播列表
     */
    public function liveList($dis,$page){

        $data = $this->where($dis)->order('roomid desc')->paginate($page)->toArray();

        return $data;
    }




    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:直播查找
     */
    public function liveFind($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:删除直播
     */
    public function liveDel($dis){

        $res = $this->where($dis)->delete();

        return $res;

    }










}